function requests(config, success, fail) {
  header = config.config || {
    "content-type": "application/json",
  }
  $.ajax({
    type: config.type,
    url: config.url,
    data: JSON.stringify(config.data),
    headers: {
      ...header
    },
    success: function (res) {
      res = JSON.parse(res)
      success(res)
    },
    fail: function (err) {
      console.log('错误信息:', err);
      fail(err)
    }
  });
}

function updateFile(config, success, fail) {
  header = config.header
  // || {
  // "content-type": "application/json",
  // }
  console.log('获取的内容:', header);
  $.ajax({
    type: config.type,
    url: config.url,
    data: config.data,
    headers: {
      ...header
    },
    processData: false,
    contentType: false,
    success: function (res) {
      res = JSON.parse(res)
      success(res)
    },
    fail: function (err) {
      console.log('错误信息:', err);
      fail(err)
    }
  });
}
var vue = new Vue({
  el: "#App",
  data: {
    key: 1,
    fileList: [],
    file_path: "",
    test: "测试内容显示?",
    select_broker: "",
    select_date: "",
    broker_arr: [],
    date_arr: [],
    isloading: false,
    tableData: []
  },
  // options
  // {
  //   value: '选项1',
  //   label: '黄金糕'
  // }
  methods: {
    handleChange() {
      // this.fileList = fileList.slice(-3);
      console.log('调用第几次??');
      console.log('输出内容:', this);
      // raw
      var esd = {}
      var myFile = this.$refs.myFiles;
      console.log('调试myFile:', myFile);
      // return
      myFile.click()
      myFile.onchange = (resd) => {
        // target.files
        // target.files[0].name
        console.log('获取触发的 打开内容:', resd);
        dataframe = new FormData()
        dataframe.append("file", resd.target.files[0])
        dataframe.append("title", resd.target.files[0].name)
        this.isloading = true
        var config = {
          type: "POST",
          url: "/postfile",
          data: dataframe,
          // header: {
          //   "content-type": "multipart/form-data",
          // }
        }
        updateFile(config, (res) => {
          console.log('获取的res的内容:', res);
          if (res.code == 200) {
            // var file = 
            console.log('写入当前列表:?');
            this.file_path = res.data.files
            this.get_all_title()
          }
          console.log('写入当前列表:?1', this.file_path);
          myFile.value = ''
          myFile.value = null
          resd.target.value = ""
          this.isloading = false
          // 
        }, (err) => {
          console.log('错误信息:', err);
          myFile.value = ''
          myFile.value = null
          resd.target.value = ""
          this.isloading = false
        })
      }
      // }
      //  "Content-Type": "multipart/form-data"
      // dataframe = new FormData()
      // dataframe.append("file", esd.raw)
    },
    fileUpdata(e) {
      console.log(e)
      var myFile = this.$refs.myFiles;
      console.log(myFile);
      console.log('获取内容:', myFile[0].files);
      files = myFile[0].files
      console.log('文件内容:', files);
      myFile.value = ''
      myFile.value = null
    },
    get_all_title() {
      var config = {
        type: 'GET',
        url: '/api/get_title_list',
        data: "",
      }
      this.isloading = true
      requests(config, (res) => {
        console.log('成功信息:', res);
        if (res.code == 200) {
          var title_list = res.data.title_list
          // value: '选项1',
          //   label: '黄金糕'
          var title_all = []
          for (var i = 0; i < title_list.length; i++) {
            var ele = title_list[i];
            var options = {
              "value": ele,
              "label": ele
            }
            title_all.push(options)
          }
          this.broker_arr = []
          this.broker_arr.push(...title_all)
        }
        this.isloading = false
        // data.title_list
      }, (err) => {
        console.log('错误信息:', err);
        this.isloading = false
      })
    },
    handleSelect1(e) {
      console.log('获取选择的内容:', e);
      var config = {
        type: 'POST',
        url: '/api/get_select_list',
        data: {
          // "leg_party": e,
          "selct_party": e
        },
      }
      this.tableData = []
      this.isloading = true
      requests(config, (res) => {
        console.log('成功信息1:', res);
        // data.date_list
        // code
        if (res.code == 200) {
          date_list = res.data.date_list || []
          var date_arr = []
          if (date_list.length > 0) {
            for (let i = 0; i < date_list.length; i++) {
              var ele = date_list[i];
              var option = {
                "value": ele,
                "label": ele
              }
              date_arr.push(option)
            }
            this.select_date = ""
            this.date_arr = []
            this.date_arr.push(...date_arr)
          }
        }
        this.isloading = false
      }, (err) => {
        console.log('错误信息1:', err);
        this.isloading = false
      })
    },
    handleSelect2(e) {
      console.log('获取时间内容:', e);
      this.tableData = []
      // 选择后时间之后 还需要判断 bond_code 的内容
      var config = {
        type: 'POST',
        url: '/api/get_table_data_list',
        data: {
          // "leg_party": e,
          "select_leg_party": this.select_broker,
          "select_date": e
        },
      }
      this.isloading = true
      requests(config, (res) => {
        console.log('成功信息:', res);
        // code
        // data.pd_arr
        if (res.code == 200) {
          var pd_arr = res.data.pd_arr
          this.setDataTable(pd_arr)
        }
        this.isloading = false
      }, (err) => {
        console.log('错误信息:', err);
        this.isloading = false
      })
    },
    setDataTable(pd_arr) {
      var tableData = []
      for (var key in pd_arr) {
        if (Object.hasOwnProperty.call(pd_arr, key)) {
          var ele = pd_arr[key];
          var options = {
            "title": key,
            "title_bp1": "",
            "title_bp2": "",
            "title_bp3": "",
            "title_bp4": "",
            "title_bp5": "",
            "title_bp6": "",
            "total": "",
          }
          for (let i = 0; i < ele.length; i++) {
            let res = ele[i];
            // data.pd_arr["200315.IB"][1].date_std
            // data.pd_arr["200315.IB"][1].yidle_std
            let yidle_std = Number(res.yidle_std)
            keys = this.format_yield(yidle_std)
            // length
            if (options[keys] > 0) {
              options[keys] = this.format_date(Number(res.date_std), options[keys])
              console.log('写入到数据?1', options[keys]);
            } else {
              options[keys] = this.format_date(Number(res.date_std))
              console.log('写入到数据?2', options[keys]);
            }
          }
          var total = ""
          for (var key in options) {
            if (Object.hasOwnProperty.call(options, key)) {
              if (key != "title" && key != "total") {
                var ele = options[key];
                console.log('获取对应计算的值:', ele, options);
                if (ele != "") {
                  total = this.add_date(ele, total)
                  console.log('获取对应的add_date:', total);
                } else {
                  continue
                }
              }
            }
          }
          options["total"] = total
          tableData.push(options)
        }
      }
      this.tableData = []
      this.tableData.push(...tableData)
      var options1 = {
        "title": "合计",
        "title_bp1": "",
        "title_bp2": "",
        "title_bp3": "",
        "title_bp4": "",
        "title_bp5": "",
        "title_bp6": "",
        "total": "",
      }
      tableDatas = JSON.parse(JSON.stringify(tableData))
      for (var i = 0; i < tableDatas.length; i++) {
        var ele = tableDatas[i];
        options1["title_bp1"] = this.add_date(options1["title_bp1"], ele["title_bp1"])
        options1["title_bp2"] = this.add_date(options1["title_bp2"], ele["title_bp2"])
        options1["title_bp3"] = this.add_date(options1["title_bp3"], ele["title_bp3"])
        options1["title_bp4"] = this.add_date(options1["title_bp4"], ele["title_bp4"])
        options1["title_bp5"] = this.add_date(options1["title_bp5"], ele["title_bp5"])
        options1["title_bp6"] = this.add_date(options1["title_bp6"], ele["title_bp6"])
      }
      options1["total"] = this.add_date(options1["total"], options1["title_bp1"])
      options1["total"] = this.add_date(options1["total"], options1["title_bp2"])
      options1["total"] = this.add_date(options1["total"], options1["title_bp3"])
      options1["total"] = this.add_date(options1["total"], options1["title_bp4"])
      options1["total"] = this.add_date(options1["total"], options1["title_bp5"])
      options1["total"] = this.add_date(options1["total"], options1["title_bp6"])
      console.log('合并后的合计:', options1);
      this.tableData.push(options1)
      console.log('遍历后的 table内容:', this.tableData);
      // title
      // title_bp1
      // title_bp2
      // title_bp3
      // title_bp4
      // title_bp5
      // title_bp6

    },
    format_yield(e) {
      var key = ""
      if (Number(e) >= 0 && Number(e) < 5) {
        key = "title_bp1"
      }
      if (Number(e) >= 5 && Number(e) < 10) {
        key = "title_bp2"
      }
      if (Number(e) >= 10 && Number(e) < 20) {
        key = "title_bp3"
      }
      if (Number(e) >= 20 && Number(e) < 30) {
        key = "title_bp4"
      }
      if (Number(e) >= 30 && Number(e) <= 50) {
        key = "title_bp5"
      }
      if (Number(e) > 50) {
        key = "title_bp6"
      }
      return key
    },
    add_date(formatdate_o = "", formatdate_n = "") {
      var timers_h = 0
      var timers_m = 0
      var res1_o = 0
      var res2_o = 0
      var res1_n = 0
      var res2_n = 0
      // data.pd_arr["180303.IB"]
      console.log('获取计算时间格式:', formatdate_o, formatdate_n);
      if (formatdate_n.indexOf("h") > -1) {
        res1_n = formatdate_n.match(/(\d+)h/g)[0].replace("h", "") || "0"
      }
      if (formatdate_n.indexOf("m") > -1) {
        res2_n = formatdate_n.match(/(\d+)m/g)[0].replace("m", "") || "0"
      }
      if (formatdate_o.indexOf("h") > -1) {
        res1_o = formatdate_o.match(/(\d+)h/g)[0].replace("h", "") || "0"
      }
      if (formatdate_o.indexOf("m") > -1) {
        res2_o = formatdate_o.match(/(\d+)m/g)[0].replace("m", "") || "0"
      }
      console.log('获取对应的内容:', res1_n, res2_n, res1_o, res2_o);
      var res1 = Number(res1_n) + Number(res1_o)
      var res2 = Number(res2_n) + Number(res2_o)
      console.log('计算后的res1:', res1);
      if (res1 > 0) {
        res2 = Number(res2) + Number(res1 * 60)
      }
      console.log('获取对应的内容1:', res2);
      if (res2 >= 60) {
        var back_u = ""
        if (res2 == 60) {
          back_u = "#1h".replace("#1", parseInt(res2 / 60))
        } else {
          back_u = "#1h#2m".replace("#1", parseInt(res2 / 60)).replace("#2", parseInt(res2 % 60))
        }
        console.log('计算结果?', back_u);
        return back_u
      } else {
        return "#2m".replace("#2", parseInt(res2))
      }
      // if (res1 > 0 || res2 > 0) {
      //   // return timers_h
      //   if (res1 > 0) {
      //     return "#1h#2m".replace("#1", res1).replace("#2", res2)
      //   } else if (res2 > 0) {
      //     // if (parseInt(res2 / 60) > 0) {
      //     //   back_u = "#1h#2m".replace("#1", parseInt(res2 / 60)).replace("#2", res2 % 60)
      //     //   return back_u
      //     // } else {
      //     return "#2m".replace("#2", res2)
      //     // }
      //   } else {
      //     return ""
      //   }
      // } else {
      //   return ""
      // }
    },
    format_date(time, formatdate = "") {
      // data.pd_arr["200315.IB"][1].date_std
      console.log('格式化的数据:', time, formatdate);
      var res1 = 0
      var res2 = 0
      var timers = 0
      var timers_h = 0
      var timers_m = 0
      if (formatdate.indexOf("h") > -1) {
        res1 = formatdate.match(/(\d+)h/g)[0]
        res1 = res1.replace("h", "")
      }
      if (formatdate.indexOf("m") > -1) {
        res2 = formatdate.match(/(\d+)m/g)[0]
        res2 = res2.replace("m", "")
      }
      console.log('未格式化?', res1, res2);
      if (time > 0) {
        timers = parseInt(time / 60)
        if (timers >= 60) {
          timers_h = parseInt(timers / 60)
          timers_m = timers % 60
        } else {
          timers_m = timers
        }
        timers_h = Number(timers_h) + Number(res1)
        timers_m = Number(timers_m) + Number(res2)
        if (timers_h > 0) {
          // return timers_h
          var timer_back = "#1h#2m".replace("#1", timers_h).replace("#2", timers_m)
          console.log('格式化后的时间:', timers_h, timers_m, res1, res2, timer_back);
          return timer_back
        } else {
          return "#2m".replace("#2", timers_m)
        }
      } else {
        return "1m"
      }
    }
  },
  mounted() {
    console.log('mounted的内容:');
    console.log('create的内容:', vue);
    // this.get_all_title()
  },
})